package com.efast.dmom.aps.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.dmom.aps.bean.ProductRoute;
import com.efast.dmom.aps.bean.ResultBean;
import com.efast.dmom.aps.datahandlers.ProductRouteDataHandler;
import com.efast.dmom.aps.service.ProductRouteService;

@Transactional
@Service
public class ProductRouteServiceImpl implements ProductRouteService {

	@Autowired
	private ProductRouteDataHandler productRouteDataHandler;

	@Override
	public ResultBean save(ProductRoute productRoute) {
		ResultBean resultBean = new ResultBean(true, ' ');
		// check required items
		if (productRoute.getInnerData().getPartId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查物料是否填写!");
			return resultBean;
		}
		if (productRoute.getInnerData().getRouteId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查工艺路线是否填写!");
			return resultBean;
		}
		ProductRoute productRouteData = productRouteDataHandler.getProductRouteByPartIdAndRouteId(
				productRoute.getInnerData().getPartId(), productRoute.getInnerData().getRouteId());
		if (productRouteData.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("产品工艺关系已存在，请重填!");
			return resultBean;
		}
		productRouteDataHandler.save(productRoute);
		resultBean.setSuccess(true);
		resultBean.setMessage("产品工艺关系保存成功!");
		return resultBean;
	}

	@Override
	public ResultBean update(ProductRoute productRoute) {
		ResultBean resultBean = new ResultBean(true, ' ');
		if (productRoute.getInnerData().getPartId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查物料是否填写!");
			return resultBean;
		}
		if (productRoute.getInnerData().getRouteId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查工艺路线是否填写!");
			return resultBean;
		}
		ProductRoute productRouteDataById = productRouteDataHandler.getById(productRoute.getInnerData().getId());
		if (productRouteDataById.getInnerData() == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("修改的数据不存在!");
			return resultBean;
		}
		if (productRoute.getInnerData().getPartId() != productRouteDataById.getInnerData().getPartId()
				&& productRoute.getInnerData().getRouteId() != productRouteDataById.getInnerData().getRouteId()) {
			ProductRoute productRouteData = productRouteDataHandler.getProductRouteByPartIdAndRouteId(
					productRoute.getInnerData().getPartId(), productRoute.getInnerData().getRouteId());
			if (productRouteData.getInnerData() != null) {
				resultBean.setSuccess(false);
				resultBean.setMessage("修改后的产品工艺关系已存在，不可进行修改!");
				return resultBean;
			}
		}
		productRouteDataHandler.update(productRoute);
		resultBean.setSuccess(true);
		resultBean.setMessage("产品工艺关系修改成功!");
		return resultBean;
	}

	@Override
	public ResultBean delete(ProductRoute productRoute) {
		ResultBean resultBean = new ResultBean(true, ' ');
		productRouteDataHandler.delete(productRoute);
		resultBean.setSuccess(true);
		resultBean.setMessage("产品工艺关系删除成功!");
		return resultBean;
	}

	@Override
	public ProductRoute getById(long id) {
		return productRouteDataHandler.getById(id);
	}

}
